//
//  XOWMUserViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/3/6.
//  Copyright © 2018年 skyline. All rights reserved.
//



#import "XOWMUserTipView.h"
#import "XOWMUserPortrait.h"
#import "XOWMUserOption.h"
#import "XOWMPDF.h"
#import "XOWMUserViewController.h"
#import "XOWMPWDViewController.h"
@interface XOWMUserViewController ()<YJDUserPortraitDelegate>
@property (nonatomic, strong) XOWMUserTipView *tipsView;
@property (nonatomic, strong) XOWMUserPortrait *portraitView;
@property (nonatomic, strong) XOWMPDF *pdfView;
@end

@implementation XOWMUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CELL_LINE_COLOR;
    
    self.portraitView.hidden = NO;
    self.naviBar.title = @"用户中心";
    // Do any additional setup after loading the view.
}

- (XOWMUserTipView *)tipsView
{
    if (!_tipsView)
    {
        _tipsView = [[XOWMUserTipView alloc] initWithText:@"建议尽快绑定账号，以防数据丢失"];
        [self.view addSubview:_tipsView];
        _tipsView.c_left.equal(self.view);
        _tipsView.c_right.equal(self.view);
        _tipsView.c_top.equal(self.naviBar.c_bottom);
        _tipsView.c_height.equal(25);
    }
    return _tipsView;
}

- (XOWMUserPortrait *)portraitView
{
    if (!_portraitView)
    {
        _portraitView = [[XOWMUserPortrait alloc] initWithName:[XOWMPlatform platform].accountID];
        _portraitView.delegate = self;
        [self.view addSubview:_portraitView];
        _portraitView.c_left.equal(self.view);
        _portraitView.c_right.equal(self.view);
        _portraitView.c_height.equal(HEIGHT);
        if (![XOWMHelper shareHelp].isBind)
            _portraitView.c_top.equal(self.tipsView.c_bottom);
        else
            _portraitView.c_top.equal(self.naviBar.c_bottom);
    }
    return _portraitView;
}

- (XOWMPDF *)pdfView
{
    if (!_pdfView)
    {
        _pdfView = [[XOWMPDF alloc] initWithPDF:nil];
        [self.view addSubview:_pdfView];
        _pdfView.c_edges.equal(UIEdgeInsetsZero);
    }
    return _pdfView;
}

- (void)yjd_setupSubView
{
    
    NSString *bind = ([XOWMHelper shareHelp].isBind)?@"已绑定":@"绑定手机";
    NSString *authenticate = ([XOWMHelper shareHelp].isAuthenticate)?@"已认证":@"实名认证";
    
    NSArray *titleArr = @[bind, authenticate,@"修改密码", @"我的消息", @"用户协议", @"用户帮助"];
    NSArray *iconArr = @[@"pwdInput",@"pwdInput",@"pwdInput", @"news", @"help", @"xieyi"];
    for (int i = 0; i < titleArr.count; i++)
    {
        int row = i / 2;
        int column = i % 2;
        XOWMUserOption *option = [[XOWMUserOption alloc] initWithTitle:titleArr[i] icon:iconArr[i]];
        [option addTarget:self action:@selector(yjd_actionForOption:) forControlEvents:UIControlEventTouchUpInside];
        option.tag = 200 + i;
        
        if ([XOWMHelper shareHelp].isBind && i == 0)
        {
            option.titileColor = [XOWMHelper shareHelp].color;
        }
        else if ([XOWMHelper shareHelp].isAuthenticate && i == 1)
        {
            option.titileColor = [XOWMHelper shareHelp].color;
        }
        
        [self.view addSubview:option];
        option.c_top.equal(self.portraitView.c_bottom).with.coffset(GAP + (USEROPTIONHEIGHT + 1) * row);
        option.c_height.equal(USEROPTIONHEIGHT);
        if (column % 2)
        {
            option.c_right.equal(self.view);
            option.c_left.equal(self.view.c_centerX).with.coffset(0.5);
        }
        else
        {
            option.c_left.equal(self.view);
            option.c_right.equal(self.view.c_centerX).with.coffset(-0.5);
        }
    }
}

- (void)yjd_actionForOption:(XOWMUserOption *)sender
{    
    if (sender.tag == 200)
    {
        if ([XOWMHelper shareHelp].isBind)
        {
            [CCAlert showMessage:@"此账号已绑定手机" title:@"提示" enter:nil];
            return;
        }
        return [self presentViewController:[NSClassFromString(@"XOWMBindViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 201)
    {
        if ([XOWMHelper shareHelp].isAuthenticate)
        {
            [CCAlert showMessage:@"此账号已实名认证" title:@"提示" enter:nil];
            return;
        }
        return [self presentViewController:[NSClassFromString(@"XOWMRZViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 202)
    {
        XOWMPWDViewController *pwd = [[XOWMPWDViewController alloc] init];
        pwd.type = YJDPWDViewTypeResetPWD;
        [self presentViewController:pwd animated:YES completion:nil];
    }
    else if (sender.tag == 203)
    {
        [CCAlert showMessage:@"此功能暂未开放" title:@"提示" enter:nil];
        return;
        [self presentViewController:[NSClassFromString(@"XOWMNewsViewController") new] animated:YES completion:nil];
    }
    else if (sender.tag == 204)
    {
        self.pdfView.pdf = @"protocol";
    }
    else if (sender.tag == 205)
    {
        self.pdfView.pdf = @"userHelp";
    }
}

#pragma mark - YJDUserPortraitDelegate 方法
- (void)yjd_methodForSwitchButton
{
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_YJDUserViewControllerDelegate)])
        {
            [self.delegate yjd_YJDUserViewControllerDelegate];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self yjd_setupSubView];
}

- (void)viewDiddAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    for (UIView *view in [self.view subviews])
    {
        if (view.tag >= 200)
        {
            [view removeFromSuperview];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_checkInfo {
    NSLog(@"Get Info Success");
}
@end
